//
//  RHParaSingleCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/15.
//

#import "RHParaSingleCell.h"
#import "DeviceChartFieldModel.h"
#import "RHOtherFieldsCell.h"

@interface RHParaSingleCell()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic, copy) NSString *deviceTypeCode;

@property (nonatomic, assign) NSInteger showCount;

@property (nonatomic, assign) CGFloat tableViewHeight;
@end
@implementation RHParaSingleCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setDataArr:(NSArray *)dataArr showCount:(NSInteger)count  deviceTypeCode:(NSString*)deviceTypeCode
{
    kWeakSelf;
    _dataArr = dataArr;
    _showCount = count;
    _deviceTypeCode = deviceTypeCode;
//    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.offset(count*40 + 8);
//    }];
    [self.tableView reloadData];
    
    
   
}

- (void)setupUI
{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
  
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0,0));
    }];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = CFTabViewBgColor;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[RHOtherFieldsCell class] forCellReuseIdentifier:@"RHOtherFieldsCell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 0)];
    _tableView.tableFooterView = [self footerView];
    _tableView.scrollEnabled = NO;
    [self.bgView addSubview:_tableView];
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    [self.tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"---->talbeView contentSize:%@",change[@"new"]);
    if ([keyPath isEqualToString:@"contentSize"]) {
        CGSize contentSize = [change[@"new"] CGSizeValue];
        if (contentSize.height != self.tableViewHeight) {
            self.tableViewHeight = contentSize.height;
            [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(contentSize.height);
            }];
            if (self.contentSizeDidChangeBlock) {
                self.contentSizeDidChangeBlock();
            }
        }
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count >= self.showCount?self.showCount:self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DeviceChartFieldModel *fieldModel = self.dataArr[indexPath.row];
    RHOtherFieldsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHOtherFieldsCell"];
    [cell setModel:fieldModel deviceTypeCode:self.deviceTypeCode];
    cell.line.hidden = (indexPath.row == (self.showCount -1));
    if (self.dataArr.count<self.showCount) {
        cell.line.hidden = (indexPath.row == self.dataArr.count-1);
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.bgView setMiddleBorder];

    return cell;
}


-(UIView*)footerView{
    UIView *footer = kView;
    footer.frame = CGRectMake(0, 0, kUI_WINDOW_W, 8);
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(15, -12, kUI_WINDOW_W-30, 20)];
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView setBottomBorder];
    [footer addSubview:bgView];
    return footer;
}


-(void)dealloc
{
    [self.tableView removeObserver:self forKeyPath:@"contentSize"];
}


@end
